package day3;

import java.util.Arrays;

public class code2 {
    public static void main(String[] args) {

    }

    public int MoreThanHalfNum_Solution(int [] array) {
        Arrays.sort(array);
        int len = array.length;
        return array[len/2];
    }

    public int MoreThanHalfNum_Solution2(int [] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int result = array[0];
        int times = 1;
        for (int i = 1;i < array.length;i++) {
            if(times != 0) {
                if(array[i] != result) {
                    times--;
                }else {
                    times++;
                }
            }else {
                result = array[i];
                times = 1;
            }
        }
        times = 0;
        for (int i = 0;i < array.length;i++) {
            if(array[i] == result) {
                times++;
            }
        }
        if(times > array.length/2) {
            return result;
        }
        return 0;
    }
}
